/**
 * TB_DOOR_DOOR
 * 
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-29 10:22:59
 *</pre>
 */
$(function() {
	door  = new Door();
	door.init();
	
	formUrl = door.formUrl;
});

(function() {
	//定义常量
	var 	_consts = {
			GRID : "#doorGrid",// 列表对象
			PAGER : "#doorPager",// 列表分页
			FORM : '#doorForm',// 表单form
			FORMGET : '#doorFormGet'// 表单form
			
	};
	/**
	 * TB_DOOR_DOOR 对象
	 * @returns {Door}
	 */
	Door = function() {
		//定义属性
	};

	/**
	 * 方法
	 */
	Door.prototype = {
		consts:	_consts,
		/**
		 * 初始化
		 */
		init : function() {
			if (this.hasInit) // 是否已初始化
				return false;
			this.hasInit = true;
			if ($(this.consts.GRID).length > 0){//列表
				this._initGridList();
				
			}
			if ($(this.consts.FORM).length > 0){//表单
				this._initForm();
				this._initData();
				this._initOffice('e');
			}
			if ($(this.consts.FORMGET).length > 0){// 明细页面office控件初始化
				this._initOffice('r');
			}
		},
		_initOffice : function(_rights){
		},

		/**
		 * 初始列表
		 */
		_initGridList : function() {
			var me = this;
			$(this.consts.GRID).GridList(
					{
						url :  __ctx+'/rdp/door/door/listJson.htm',
						pager :this.consts.PAGER,
						colNames: ['DOORID','控制器SN','门名称 ','门编号','门控制方式','开门延时(s)','控制描述','控制器名称','管理'],
				        colModel: [{
				                 	   name:'doorid',
				                	   index: 'DOORID',
				                	   hidden:true,
				                	   key:true
				                	}, 
				                	{
				                 	   name:'controllerid',
				                	   index: 'CONTROLLERID',
				                   	   hidden:true,

									 },
									 
			                	 	 {
				                 	   name:'doorname',
				                	   index: 'DOORNAME'

										
				                	 },				                	 	
				                	 					                	 	
				                	 {
				                 	   name:'doorno',
				                	   index: 'DOORNO'

										
				                	 }, 
				                	 {
				                 	   name:'doorcontrol',
				                	   index: 'DOORCONTROL',
				                	   formatter:function(cellValue){
				                		   if(cellValue=='2'){
				                			   return '常闭';
				                		   }else if(cellValue=='3'){
				                			   return '在线';
				                		   }else if(cellValue=='1'){
				                			   return '常开';
				                		   }else{
				                			   return "";
				                		   }				                		   
				                	   }

									}, 
									{
				                 	   name:'doordelay',
				                	   index: 'DOORDELAY'

										
				                	}, 
				                	{
				                 	   name:'doorenabled',
				                	   index: 'DOORENABLED',
				                	   formatter:function(cellValue, options, rowObject){
				                		   var sn = rowObject.controllerid;
				                		   var desc ="";
				                		   if(cellValue.startsWith('1')){
				                			   desc = desc +  "进门:开启";
				                		   }else{
				                			   desc  = desc +  "进门:禁止  ";
				                		   } 
				                		   if(sn.startsWith('4')){
				                			   return desc;
				                		   }else{
				                			   desc  = desc +  " | ";
				                		   }
				                		   if(cellValue.endsWith('1')){
				                			   desc  = desc +  "出门:开启"	;			                				   
				                		   } else{
				                			   desc  = desc +  "出门:禁止";
				                		   }
				                		   return desc;
				                	    }
  
				                	 },
										
									 {
					                 	   name:'controllername',
					                	   index: 'CONTROLLERNAME'

											
					                 },
								     {
											name : '__manage',
											width : 30,
											sortable:false,
											classes:'rowOps',
											formatter : 'manage',
											formatoptions :[{
												label:'编辑',
												classes:'btn btn-primary fa fa-edit',
												action:__ctx+'/rdp/door/door/edit.htm?doorid={id}'
											}]
								    } 
						]
	
					});
		},
		
		/**
		 * 初始化表单
		 */
		_initForm : function() {
			var me = this, form = $(this.consts.FORM), frm = form.form();
			me.formUrl = new com.lc.form.FormData(form);
			// 触发表单验证
			frm.valid();
			// 处理表单保存
			$(document).on('click', 'a.fa-save', function() {
				var $el = $(this);
				$el.button('loading');
				// office提交
        		OfficePlugin.submit();
				me.formUrl.submit(me._showResponse, $el);
			});
		},
		/**
		 * 初始化数据
		 */
		_initData : function(){
			if(!$.isEmpty(frameElement) 
				&& !$.isEmpty(frameElement.dialog) 
				&& !$.isEmpty(frameElement.dialog.params)
				&& !$.isEmpty(frameElement.dialog.params.data)){
				var data = frameElement.dialog.params.data;
				this.formUrl.setData("[name^='m:']", data);
				AttachementControl.initDataTo($("div[name='div_attachment_container']")); //附件初始化
			}else{
				$("[name^='m:']", $(_consts.FORM)).each(function(){
					var data = $(this);
	        		// 单选框特殊处理 
	            	if(data.is('input') && "radio" == data.attr("type")){
			        		var defaultVal = data.attr("defaultVal");
			        		var defaultValue = data.attr("defaultValue");
		            		if(defaultValue=="true"&&defaultVal=="true"){
		            			data.prop("checked", "checked");
		            		}
		        	}
		        });
			}
			
			this.formUrl.validate();
		},
		/**
		 * 表单成功返回信息
		 * 
		 * @param responseText
		 */
		_showResponse : function(responseText) {
			var msg = new com.lc.form.ResultMessage(responseText);
			if (msg.isSuccess()) {
				DialogUtil.confirm(msg.getMessage() + ',是否继续操作',
					function(rtn) {
					if(rtn)
						window.location.reload(true);
					else
						window.location.href = __ctx+'/rdp/door/door/list.htm';
					});
			} else {
				DialogUtil.error(msg.getMessage());
			}
		}
	};
})();